using System;
using System.Collections.Generic;
using System.Text;

namespace Hermes.Framework.Implementation.File
{
    public class TxFileCopy : TxOperationTemplate, ITransactableCopy
    {
        public TxFileCopy(string srcFileName, string dstFileName)
            : base(srcFileName, dstFileName)
        {

        }

        #region ITransactableCopy

        public void Copy()
        {
            base.Execute(srcFileName, dstFileName);
        }

        #endregion

        protected override void Operate()
        {
            if (this.fileSystem.Exists(this.shadowFileName))
            {
                this.fileSystem.Delete(this.shadowFileName);
            }
            this.fileSystem.Copy(this.srcFileName, this.shadowFileName);
        }

        public override void Commit()
        {
            if (this.fileSystem.Exists(this.dstFileName))
            {
                this.fileSystem.Delete(this.dstFileName);
            }
            this.fileSystem.Rename(this.shadowFileName, this.dstFileName);
        }

        public override void Rollback()
        {
            this.fileSystem.Delete(this.shadowFileName);
        }
    }
}
